package br.com.j7.jdesk.ui.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.com.j7.jdesk.persistence.entity.Contacts;
import br.com.j7.jdesk.persistence.repository.ContactRepository;
import br.com.j7.jdesk.persistence.repository.impl.ContactRepositoryImpl;

@ManagedBean
@RequestScoped
public class ContactBean extends WebContext implements Bean{

	private Contacts contact = new Contacts();
	private List<Contacts> contacts = new ArrayList<>();
	private ContactRepository repository;
	
	public ContactBean() {
		repository =  new ContactRepositoryImpl();
	}
	
	@Override
	public String adicionar() {
		try{
			if( contact == null || contact.getIdContact().equals(0) ){
				contact.setIdContact(null);
				repository.add(contact);
				addMessage("ui.mobile.contact.add", "ui.mobile.contact.thanks");
			}else {
				repository.update(contact);
				addMessage("ui.mobile.contact.update", "ui.mobile.contact.thanks");
			}
			contact = null;
			contacts = null;
			return "home";
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public String remover() {
		// TODO Auto-generated method stub
		return "home";
	}

	@Override
	public String toForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String toList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Contacts getContact() {
		return contact;
	}
	
	public void setContact(Contacts contact) {
		this.contact = contact;
	}
	
	public List<Contacts> getContacts() {
		try {
			if(contacts == null){
				contacts = repository.list();
			}
			return contacts;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	

}
